% This is the Main script that calls getCOM_Master.m.
%
% This script as well as getCOM.m resides in, and is executed from, 
% the repository.
% 
%

% UPDATE 31 AUG 2014: I used to manually write down all the start/stop 
% frames and x,y bounding box coordinates for every molecule that I wanted
% to analyze in getCOM_Master. This was time-consuming. A few things were 
% done to streamline the process.
% 
% 1. I wrote an ImageJ macro that would store all the start/stop frames
%    and x,y bounding box coordinates into a text file (log.txt). This file
%    would normally be saved on the desktop for ease of use.
%
% 2. This script imports log.txt, stores it as the variable ImageJ_log_dir,
%    and parses the appropriate parameters to pass to getCOM_Master: 
%    
%    getCOM_Master(video filename, start frame, stop frame, 
%                  lower x-coordinate, lower y-coordinate,
%                  upper x-coordinate, upper y-coordinate,
%                  letter, comments)
%                  
%     where 'letter' is appended to the name of the .mat files that are
%     ouputs of getCOM_Master, e.g. 28AUG2014_1a, 29AUG2014_1b, etc.
%     
%     NOTE: The save_dir must be the same directory where the .fits video
%     file is located; otherwise, getCOM_Master will throw an error.


% ########################################################################
% ########################### START PROGRAM ##############################
% ########################################################################
% Query user to select the directory where the output .mat files will be 
% saved.

%% The following path is for Lab Optiplex computer (Windows):
save_dir = uigetdir('C:\Users\nanofluidics\Dropbox\CurrentAnalysis',... 
         ['Select the directory where the output .mat files will be '...
          'saved. Must be the same directory as the .fits file.']);

% Query user to select the ImageJ log file.                           
[filename, ImageJ_log_dir, FilterIndex] = ...
         uigetfile('C:\Users\nanofluidics\Dropbox\CurrentAnalysis\*.txt',... 
                   'Select the ImageJ log file to open'); 

%% The following path is for Lenovo Thinkpad computer (Windows):
% save_dir = uigetdir('C:\Users\Daniel\Dropbox\CurrentAnalysis',... 
%          ['Select the directory where the output .mat files will be '...
%           'saved. Must be the same directory as the .fits file.']);

% % Query user to select the ImageJ log file.                           
% [filename, ImageJ_log_dir, FilterIndex] = ...
%          uigetfile('C:\Users\Daniel\Dropbox\CurrentAnalysis\*.txt',... 
%                    'Select the ImageJ log file to open');

%% The following path is for Optiplex computer (Windows):
% save_dir = uigetdir('D:\Dropbox\CurrentAnalysis',... 
%          ['Select the directory where the output .mat files will be '...
%           'saved. Must be the same directory as the .fits file.']);
% 
% % Query user to select the ImageJ log file.                           
% [filename, ImageJ_log_dir, FilterIndex] = ...
%          uigetfile('D:\Dropbox\CurrentAnalysis\*.txt',... 
%                    'Select the ImageJ log file to open'); 

%%                               
% Query user to select the appropriate video.
video_filename = uigetfile(strcat(save_dir, '/*.fits'),...
                           'Select the .fits video file to analyze');
                      
% Write a date-timestamp header to the QACheck file. Change to save_dir.
cd(save_dir);
QAcheckfile = strcat(video_filename(1:end-5), 'getCOMCheck.txt');
fileID = fopen(QAcheckfile, 'a');
fprintf(fileID, '--------------------------\n');
fprintf(fileID, strcat(datestr(now, 'mmm dd, yyyy HH:MM'), ':\n'));
fprintf(fileID, '==========================\n');
fclose(fileID);

% Load the ImageJ log file.                 
cd(ImageJ_log_dir);
ImageJ_log = load(filename);

% Main program begins.
start_ind = 1:2:size(ImageJ_log,1);
end_ind = 2:2:size(ImageJ_log,1);

% Loop through each molecule in ImageJ_log and call getCOM_Master using
% the appropriate parameters for the frame start/stop and x,y coordinates
% of the bounding box.
for n=1:(size(ImageJ_log,1)/2)
    letter = char(n-1+'a');
    tmp = [ImageJ_log(start_ind(n),1), ImageJ_log(end_ind(n),1),...
           ImageJ_log(start_ind(n),2), ImageJ_log(end_ind(n),2),...
           ImageJ_log(end_ind(n),3), ImageJ_log(start_ind(n),3)];
       
    str_tmp = num2str(tmp');

    % Pass the arguments to getCOM_Master.    
    %% The following path is for Lab Optiplex computer (Windows):
    cd('C:\Users\nanofluidics\Dropbox\CODE\Nanofluidics_Code\BlueWolves\')
    
    %% The following path is for Lenovo Thinkpad computer (Windows):
    %cd('C:\Users\Daniel\Dropbox\CODE\Nanofluidics_Code\BlueWolves\')
    
    %% The following path is for Optiplex computer (Windows):
    %cd('D:\Dropbox\CODE\Nanofluidics_Code\BlueWolves\')

    %%    
    getCOM_Master(video_filename, ...
                  tmp(1), tmp(2), tmp(3), tmp(4), tmp(5), tmp(6), ...
                  letter, save_dir, ...
                  strcat('frms,x,y:',str_tmp(1,:), ',', ...
                  str_tmp(2,:),',',str_tmp(3,:),',',str_tmp(4,:), ',', ...
                  str_tmp(5,:),',',str_tmp(6,:),';20mMTE'));
end

% Change directory back to the respository.
%cd('/users/daniel_s_kim/dropbox/code/nanofluidics_code/bluewolves')